//
//  LZMyGroupBuyTableViewCell.m
//  laziz_Consumer
//
//  Created by ZhaoDongBo on 2016/11/21.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyGroupBuyTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "LZMyCouponsModel.h"
#import "Masonry.h"


@implementation LZMyGroupBuyTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self configView];
        [self addAutoLayout];
        
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
    self.bgImageView.backgroundColor = highlighted ? UIColorFromRGB(0xdedede, 1) :  [UIColor whiteColor];
}

- (void)setModel:(LZMyCouponsModel *)model {
    _model = model;
    if (model == nil) {
        return;
    }
    
    [self.titleImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",APIBaseImageUrl,self.model.userPhoto]] placeholderImage:[UIImage imageNamed:@"lz_user_placehoiderImage"]];
    self.titleLabel.text = [NSString stringWithFormat:@"%@", self.model.nickName];
    self.subLable.text = [NSString stringWithFormat:@"%@", self.model.promotionName];
    self.numberLabel.text = [NSString stringWithFormat:@"%@/%@", self.model.peopleCount, self.model.grouponCount];

}


- (void)configView {
    
    [self.contentView addSubview:self.bgImageView];
    
    [self.bgImageView addSubview:self.titleImageView];
    [self.bgImageView addSubview:self.titleLabel];
    [self.bgImageView addSubview:self.accessoryImageView];
    [self.bgImageView addSubview:self.subLable];

    [self.bgImageView addSubview:self.line];
    [self.bgImageView addSubview:self.tuanImageView];
    [self.bgImageView addSubview:self.numberLabel];
    
    // ******************** 添加阴影效果 ********************
    [self initLayerShadow];
}

- (void)addAutoLayout {

    WS(weakSelf);
    
    [self.bgImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.bottom.mas_equalTo(-10);
    }];
    
    [self.titleImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.leading.mas_equalTo(15);
        make.size.mas_equalTo(CGSizeMake(34, 34));
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(weakSelf.titleImageView.mas_trailing).with.offset(10);
        make.centerY.mas_equalTo(weakSelf.titleImageView.mas_centerY);
        make.trailing.mas_equalTo(weakSelf.accessoryImageView.mas_leading).with.offset(-15);
    }];
    
    [self.accessoryImageView makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.titleImageView.mas_centerY);
        make.trailing.mas_equalTo(-15);
        make.size.mas_equalTo(CGSizeMake(12, 12));
    }];
    
    [self.subLable makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.titleImageView.mas_bottom).with.offset(10);
        make.leading.mas_equalTo(15);
        make.trailing.mas_equalTo(-15);
        make.bottom.mas_equalTo(-51);
    }];
    
    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(0);
        make.trailing.mas_equalTo(0);
        make.bottom.mas_equalTo(-48);
        make.height.mas_equalTo(0.5);
    }];
    
    [self.tuanImageView makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(15);
        make.centerY.mas_equalTo(weakSelf.mas_bottom).with.offset(-35);
        make.size.mas_equalTo(CGSizeMake(18, 16));
    }];
    
    [self.numberLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(weakSelf.tuanImageView.mas_trailing).with.offset(9);
        make.centerY.mas_equalTo(weakSelf.tuanImageView.mas_centerY);
        make.trailing.mas_equalTo(weakSelf.mas_centerX);
    }];
}

- (void)initLayerShadow {
    // 加阴影效果
    self.bgImageView.layer.masksToBounds = NO;
    // 阴影颜色
    self.bgImageView.layer.shadowColor = [UIColor blackColor].CGColor;
    //设置阴影的高度
    self.bgImageView.layer.shadowOffset = CGSizeMake(2, 2.5);
    //设置透明度
    self.bgImageView.layer.shadowOpacity = 0.05;
    //阴影半径，默认3
    self.bgImageView.layer.shadowRadius = 2;
}

#pragma mark - Setter/Getter

- (UIImageView *)bgImageView{

    if (_bgImageView == nil) {
        
        _bgImageView = [[UIImageView alloc] init];
        _bgImageView.layer.masksToBounds    = YES;
        _bgImageView.layer.cornerRadius     = 6.0;
        _bgImageView.layer.borderWidth      = 0.5;
        _bgImageView.layer.borderColor      = UIColorFromRGB(0xededed, 1).CGColor;
    }
    
    return _bgImageView;
}

- (UIImageView *)titleImageView {

    if (_titleImageView == nil) {
        _titleImageView = [[UIImageView alloc] init];
        _titleImageView.layer.masksToBounds     = YES;
        _titleImageView.layer.cornerRadius      = 34/2.0;
    }
    
    return _titleImageView;
}

- (UILabel *)titleLabel {

    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:15];
        _titleLabel.textColor = UIColorFromRGB(0x666666, 1);
    }
    
    return _titleLabel;
}

- (UIImageView *)accessoryImageView {

    if (_accessoryImageView == nil) {
        _accessoryImageView = [[UIImageView alloc] init];
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar) {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_ar"];
        } else {
            _accessoryImageView.image = [UIImage imageNamed:@"lz_cell_accessoryimage_en"];
        }
    }
    
    return _accessoryImageView;
}

- (UILabel *)subLable {

    if (_subLable == nil) {
        _subLable = [[UILabel alloc] init];
        _subLable.numberOfLines = 2;
        _subLable.font = [UIFont systemFontOfSize:15];
        _subLable.textColor = UIColorFromRGB(0x666666, 1);
    }
    
    return _subLable;
}

- (UIView *)line {

    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xededed, 1);
    }
    
    return _line;
}

- (UIImageView *)tuanImageView {

    if (_tuanImageView == nil) {
        _tuanImageView = [[UIImageView alloc] init];
        _tuanImageView.image = [UIImage imageNamed:@"首页_分组"];
    }
    
    return _tuanImageView;
}

- (UILabel *)numberLabel {

    if (_numberLabel == nil) {
        _numberLabel = [[UILabel alloc] init];
        _numberLabel.text = @"1/10";
        _numberLabel.font = [UIFont systemFontOfSize:15];
    }
    
    return _numberLabel;
}

@end
